'use strict';
	
	angular.module('appcenter.center', [])
	.controller('centerCtrl', ['$rootScope', '$scope', 'BizData'
		,  function($rootScope, $scope, BizData) {

		var Model = this;
		/*
		console.log('${path}-->','${path}');
		var path = $('#path').val();
		console.log('path-->',path);
		console.log('$scope.path-->',$scope.path);
		*/

		//待办事项
		Model.getTodoList = function(){
			$('#todoList').LoadingOverlay("show");
			var options = {
					offset:0,
					limit:10,
					filter:"(FlowTask.sKindID='tkTask' OR FlowTask.sKindID='tkNotice') AND FlowTask.sStatusID='tesReady'",//尚未处理
					orderBy:"FlowTask.sCreateTime DESC"
			};
			
			$.ajax({
	             type: "POST",
	             url: $rootScope.getServiceName() + "/flow/flowTask/queryList",
	             data: JSON.stringify(options),
	             dataType: "json",
			     contentType: "application/json; charset=utf-8",
	             success: function(data, textStatus){
	 				//console.log('data-->',data);
					$('#todoList').LoadingOverlay("hide");
					$scope.cdataTodoList = data;
               },error: function(XMLHttpRequest, textStatus, errorThrown){
            	   $('#todoList').LoadingOverlay("hide");
               }
	         });
			
		};
		
		Model.getDoneList = function(){
			$('#doneList').LoadingOverlay("show");
			var options = {
					offset:0,
					limit:10,
					filter:"FlowTask.sKindID='tkTask' AND FlowTask.sStatusID='tesFinished' AND FlowTask.sKindID='tkTask'",//完成
					orderBy:"FlowTask.sActualFinishTime DESC"
			};
			
			$.ajax({
	             type: "POST",
	             url: $rootScope.getServiceName() + "/flow/flowTask/queryList",
	             data: JSON.stringify(options),
	             dataType: "json",
			     contentType: "application/json; charset=utf-8",
	             success: function(data, textStatus){
	 				//console.log('data-->',data);
					$('#doneList').LoadingOverlay("hide");
					$scope.cdataDoneList = data;
               },error: function(XMLHttpRequest, textStatus, errorThrown){
            	   $('#doneList').LoadingOverlay("hide");
               }
	         });
		};
		
		Model.yyzxClick = function(){
			$scope.tabpanel = 'yyzx';

			//装载数据
			Model.getTodoList();
			Model.getDoneList();
		};

		////////////////////////////////////////////////////////////////////////////////
		////////////////////////////////////////////////////////////////////////////////
		
		Model.jygkClick = function(){
			$scope.tabpanel = 'jygk';

		};

		//初始化运行
		//默认显示应用中心
		$scope.tabpanel = 'yyzx';
		//默认显示应用中心数据
//		Model.yyzxClick();
		
		//接收窗口被激活事件的广播
		$scope.$on('activeTab',function(event,data){
			console.log('接收窗口被激活事件的广播activeTab.event-->',event);
			console.log('接收窗口被激活事件的广播activeTab.data-->',data);
			//if(data.winid==$rootScope.winid){
			if(data.winid=='home'){
				console.log('刷新homecenter数据');
				//刷新数据
//				if($scope.tabpanel=='yyzx'){
//					Model.yyzxClick();
//				}
//				if($scope.tabpanel=='jygk'){
//					Model.jygkClick();
//				}
			}
		});
		
		
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		/////////////////////////工作流开始////////////////////////////////////////////////////////////////////////////////////
		
		//流程中心
		var options = {};
		options.container = Model;
		options.concept = 'FlowTask';
		options.name = 'dataFlowTask';
		options.url = $rootScope.path;
		options.httpUrl = $rootScope.getServiceName()
		options.creater = '';
		options.reader = '/flow/flowTask/query';
		options.writer = '';
		options.deleter = '';
		options.idColumnDefine = "fID";
		options.idColumnName = "fID";
		options.defCols = {fID:{name:"fID",type:"String",label:""},sFlowID:{name:"sFlowID",type:"String",label:""},sParentID:{name:"sParentID",type:"String",label:""},sCode:{name:"sCode",type:"String",label:""},sName:{name:"sName",type:"String",label:""},sContent:{name:"sContent",type:"String",label:""},sCatalogID:{name:"sCatalogID",type:"String",label:""},sKindID:{name:"sKindID",type:"String",label:""},sTypeID:{name:"sTypeID",type:"String",label:""},sTypeName:{name:"sTypeName",type:"String",label:""},sProcess:{name:"sProcess",type:"String",label:""},sActivity:{name:"sActivity",type:"String",label:""},sCURL:{name:"sCURL",type:"String",label:""},sEURL:{name:"sEURL",type:"String",label:""},sCreatorPersonID:{name:"sCreatorPersonID",type:"String",label:""},sCreatorPersonName:{name:"sCreatorPersonName",type:"String",label:""},sCreatorPosID:{name:"sCreatorPosID",type:"String",label:""},sCreatorPosName:{name:"sCreatorPosName",type:"String",label:""},sCreatorDeptID:{name:"sCreatorDeptID",type:"String",label:""},sCreatorDeptName:{name:"sCreatorDeptName",type:"String",label:""},sCreatorOgnID:{name:"sCreatorOgnID",type:"String",label:""},sCreatorOgnName:{name:"sCreatorOgnName",type:"String",label:""},sCreatorFID:{name:"sCreatorFID",type:"String",label:""},sCreatorFName:{name:"sCreatorFName",type:"String",label:""},sExecutorPersonID:{name:"sExecutorPersonID",type:"String",label:""},sExecutorPersonName:{name:"sExecutorPersonName",type:"String",label:""},sExecutorPosID:{name:"sExecutorPosID",type:"String",label:""},sExecutorPosName:{name:"sExecutorPosName",type:"String",label:""},sExecutorDeptID:{name:"sExecutorDeptID",type:"String",label:""},sExecutorDeptName:{name:"sExecutorDeptName",type:"String",label:""},sExecutorOgnID:{name:"sExecutorOgnID",type:"String",label:""},sExecutorOgnName:{name:"sExecutorOgnName",type:"String",label:""},sExecutorFID:{name:"sExecutorFID",type:"String",label:""},sExecutorFName:{name:"sExecutorFName",type:"String",label:""},sData1:{name:"sData1",type:"String",label:""},sData2:{name:"sData2",type:"String",label:""},sData3:{name:"sData3",type:"String",label:""},sData4:{name:"sData4",type:"String",label:""},sStatusID:{name:"sStatusID",type:"String",label:""},sStatusName:{name:"sStatusName",type:"String",label:""},sCreateTime:{name:"sCreateTime",type:"Datetime",label:""},sLastModifyTime:{name:"sLastModifyTime",type:"Datetime",label:""},sExpectStartTime:{name:"sExpectStartTime",type:"Datetime",label:""},sExpectFinishTime:{name:"sExpectFinishTime",type:"Datetime",label:""},sActualStartTime:{name:"sActualStartTime",type:"Datetime",label:""},sActualFinishTime:{name:"sActualFinishTime",type:"Datetime",label:""},sExecuteMode:{name:"sExecuteMode",type:"String",label:""},sExecuteMode2:{name:"sExecuteMode2",type:"String",label:""},version:{name:"version",type:"Integer",label:""}};
		options.isTree = false;
		options.parentRelation = 'fParent';
		options.nodeKindRelation = 'nodeKind';
		options.nodeLevelRelation = 'nodeLevel';
		options.pathRelation = '';
		options.master = {"data":"","relation":""};
		options.directDeleteMode = true;
		options.limit = 10;
		options.orderBy = "sCreateTime,sFlowID DESC";

		$rootScope.dataFlowTask = new BizData(options);
		
		//增加过滤器，只显示本人及本公司的数据
		$rootScope.dataFlowTask.filter = "FlowTask.sKindID='tkTask' AND FlowTask.sStatusID='tesReady'"
		;
		
		$rootScope.refresh = function(){
			$rootScope.dataFlowTask.refreshData();
//				alert('ladskjfla;dfs');
		}
		

		//创建工作流实例 
		$rootScope.createProcess = function(){
			var data = new Object();
			data.type = 'info';
			data.approved = true;//显示办理人
			var modalInstance = systemService.dialogBillDo(data);
			
			var url = '';
			
	        //模态窗口关闭之后执行的事件
	        modalInstance.result.then(function (event) {
	            //成功关闭窗口
	        	event.url = url;
	        	event.sData1 = '11111111111111111111111';//Model.dataMaster.getCurrentID();
//		        	systemService.doBill(event);
	        	console.log('event-->',event);
	        	
				$.LoadingOverlay("show");
//				$http.post($rootScope.getServiceName()+'/flow/flowTask/createProcess',event)
//				.success(function(data,status,headers,config){
//					console.log('data-->',data);
//					$.LoadingOverlay("hide");
//					alert('创建工作流实例成功！id:'+data.id);
//				}).error(function(data,status,headers,config){
//					$.LoadingOverlay("hide");
//					alert('$http调用失败！/n/r'+data.errText);
//				});
				
				$.ajax({
		             type: "POST",
		             url: $rootScope.getServiceName() + "/flow/flowTask/createProcess",
		             data: JSON.stringify(event),
		             dataType: "json",
				     contentType: "application/json; charset=utf-8",
		             success: function(data, textStatus){
		 				console.log('data-->',data);
						$('#show').LoadingOverlay("hide");
						alert('创建工作流实例成功！id:'+data.id);
	               },error: function(XMLHttpRequest, textStatus, errorThrown){
	            	   $('#show').LoadingOverlay("hide");
	            	   alert('ajax调用失败！/n/r'+data.errText);
	               }
		         });

	        }, function (reason) {
	            console.info('Modal dismissed at: ' + new Date());  
	        });  
			
			console.log('创建工作流实例');
		}
		
		//创建工作流任务 
		$rootScope.createTask = function(){
			var data = new Object();
			data.type = 'info';
			data.approved = true;//显示办理人
			var modalInstance = systemService.dialogBillDo(data);
			
			var url = '';
			
	        //模态窗口关闭之后执行的事件
	        modalInstance.result.then(function (event) {
	            //成功关闭窗口
	        	event.url = url;
	        	event.parentID = $rootScope.dataFlowTask.getCurrentID();
//		        	systemService.doBill(event);
	        	console.log('event-->',event);
	        	
				$.LoadingOverlay("show");
//				$http.post($rootScope.getServiceName()+'/flow/flowTask/createTask',event)
//				.success(function(data,status,headers,config){
//					console.log('data-->',data);
//					$.LoadingOverlay("hide");
//					alert('创建工作流任务成功！id:'+data.id);
//				}).error(function(data,status,headers,config){
//					$.LoadingOverlay("hide");
//					alert('$http调用失败！/n/r'+data.errText);
//				});
				
				$.ajax({
		             type: "POST",
		             url: $rootScope.getServiceName() + "/flow/flowTask/createTask",
		             data: JSON.stringify(event),
		             dataType: "json",
				     contentType: "application/json; charset=utf-8",
		             success: function(data, textStatus){
		 				console.log('data-->',data);
						$('#show').LoadingOverlay("hide");
						alert('创建工作流任务成功！id:'+data.id);
	               },error: function(XMLHttpRequest, textStatus, errorThrown){
	            	   $('#show').LoadingOverlay("hide");
	            	   alert('ajax调用失败！/n/r'+data.errText);
	               }
		         });

	        }, function (reason) {
	            console.info('Modal dismissed at: ' + new Date());  
	        });  
			
			console.log('创建工作流任务');
		}
		
		//创建工作流通知 
		$rootScope.createNotice = function(){
			var data = new Object();
			data.type = 'info';
			data.approved = true;//显示办理人
			var modalInstance = systemService.dialogBillDo(data);
			
			var url = '';
			
	        //模态窗口关闭之后执行的事件
	        modalInstance.result.then(function (event) {
	            //成功关闭窗口
	        	event.url = url;
	        	event.parentID = $rootScope.dataFlowTask.getCurrentID();
//		        	systemService.doBill(event);
	        	console.log('event-->',event);
	        	
				$.LoadingOverlay("show");
//				$http.post($rootScope.getServiceName()+'/flow/flowTask/createNotice',event)
//				.success(function(data,status,headers,config){
//					console.log('data-->',data);
//					$.LoadingOverlay("hide");
//					alert('创建工作流通知成功！id:'+data.id);
//				}).error(function(data,status,headers,config){
//					$.LoadingOverlay("hide");
//					alert('$http调用失败！/n/r'+data.errText);
//				});
				
				$.ajax({
		             type: "POST",
		             url: $rootScope.getServiceName() + "/flow/flowTask/createNotice",
		             data: JSON.stringify(event),
		             dataType: "json",
				     contentType: "application/json; charset=utf-8",
		             success: function(data, textStatus){
		 				console.log('data-->',data);
						$('#show').LoadingOverlay("hide");
						alert('创建工作流通知成功！id:'+data.id);
	               },error: function(XMLHttpRequest, textStatus, errorThrown){
	            	   $('#show').LoadingOverlay("hide");
	            	   alert('ajax调用失败！/n/r'+data.errText);
	               }
		         });

	        }, function (reason) {
	            console.info('Modal dismissed at: ' + new Date());  
	        });  
			
			console.log('创建工作流通知');
		}

		//创建工作流实例和当前任务（启动工作流） 
		
		$rootScope.startClick = function(row){
			console.log('启动流程');
			console.log('row-->',row);
			
			var data = new Object();
			data.type = 'info';
			data.approved = true;//显示办理人
			data.enterpriseNo = row.enterpriseNo.value;
			
			var modalInstance = systemService.dialogBillDo(data);
			
			var url = '';
			
	        //模态窗口关闭之后执行的事件
	        modalInstance.result.then(function (event) {
	            //成功关闭窗口
	        	event.url = url;
	        	event.sData1 = row.fID.value;//'222222222222222222222222222';//Model.dataMaster.getCurrentID();
	        	event.sData2 = row.billType.value;
	        	event.sData3 = row.billTypeName.value;
	        	event.systemCode = row.systemCode.value;
	        	
				$.LoadingOverlay("show");
//				$http.post($rootScope.getServiceName()+'/flow/flowTask/start',event)
//				.success(function(data,status,headers,config){
//					$.LoadingOverlay("hide");
//					$rootScope.closeCurrentWindow();
//				}).error(function(data,status,headers,config){
//					$.LoadingOverlay("hide");
//					alert('$http调用失败：'+status);
//				});
				
				$.ajax({
		             type: "POST",
		             url: $rootScope.getServiceName() + "/flow/flowTask/start",
		             data: JSON.stringify(event),
		             dataType: "json",
				     contentType: "application/json; charset=utf-8",
		             success: function(data, textStatus){
		 				console.log('data-->',data);
						$('#show').LoadingOverlay("hide");
						$rootScope.closeCurrentWindow();
	               },error: function(XMLHttpRequest, textStatus, errorThrown){
	            	   $('#show').LoadingOverlay("hide");
	            	   alert('ajax调用失败！/n/r'+data.errText);
	               }
		         });

	        }, function (reason) {
	            console.info('Modal dismissed at: ' + new Date());  
	        });  
			
//			console.log('创建工作流实例');
		};
		
		
		
		//流程流转 
		$rootScope.flowClick = function(event){
			console.log('流程流转');
			var row = event.row;
			console.log('row-->',row);
			
			var data = new Object();
			data.type = 'info';
			
			if(event.action=='FINISH'){
				data.approved = false;//不显示办理人
			}else if(event.action=='FORWARD'){
				data.approved = true;//显示办理人
			}else if(event.action=='BACKWARD'){
				data.approved = false;//不显示办理人
			}else if(event.action=='ABORT'){
				data.approved = false;//不显示办理人
			}else{
				data.approved = false;//不显示办理人
			}
			
			if(event.parentID==null || event.parentID==undefined || event.parentID==''){
				alert('任务ID不能为空，请从待办事项中进入！');
				return;
			}
			
			data.enterpriseNo = row.enterpriseNo.value;
			
//				data.approved = true;//显示办理人
			var modalInstance = systemService.dialogBillDo(data);
			
	        //模态窗口关闭之后执行的事件
	        modalInstance.result.then(function (params) {
	            //成功关闭窗口
	        	params.parentID = event.parentID;
	        	params.action = event.action;
	        	params.sData1 = event.sData1;//'222222222222222222222222222';//Model.dataMaster.getCurrentID();
	        	params.sData2 = event.sData2;
	        	params.sData3 = event.sData3;
	        	console.log('params-->',params);
	        	
				$.LoadingOverlay("show");
//				$http.post($rootScope.getServiceName()+'/flow/flowTask/flow',params)
//				.success(function(data,status,headers,config){
//					console.log('data-->',data);
//					$.LoadingOverlay("hide");
//					$rootScope.closeCurrentWindow();
//				}).error(function(data,status,headers,config){
//					$.LoadingOverlay("hide");
//					alert('$http调用失败！/n/r'+data.errText);
//				});
				
				$.ajax({
		             type: "POST",
		             url: $rootScope.getServiceName() + "/flow/flowTask/flow",
		             data: JSON.stringify(params),
		             dataType: "json",
				     contentType: "application/json; charset=utf-8",
		             success: function(data, textStatus){
		 				console.log('data-->',data);
						$('#show').LoadingOverlay("hide");
						$rootScope.closeCurrentWindow();
	               },error: function(XMLHttpRequest, textStatus, errorThrown){
	            	   $('#show').LoadingOverlay("hide");
	            	   alert('ajax调用失败！/n/r'+data.errText);
	               }
		         });

	        }, function (reason) {
	            console.info('Modal dismissed at: ' + new Date());  
	        });  
		};

		//流程轨迹信息
		$rootScope.openFlowTaskTrack = function(event){
//			systemService.openFlowTaskTrackDialog(event);
			console.log('$rootScope.openFlowTaskTrack.event-->',event);
	        var dlg = $modal.open({  
	        	backdrop: 'static',
	        	size: 'lg',
	            templateUrl: $rootScope.getProjectName() + '/flow/flowTaskTrack/monitor',  
	            controller: 'flowTaskTrackMonitorCtrl as Model',  
	            resolve: {
	                params: function () {  
	                    return angular.copy(event);  
	                }
                    ,title : function() { return '流程信息'; }
                    ,loadCtrl : $ocLazyLoad.load('js/flow/flowTaskTrack/monitor.js')
		        }
	        });  
			
		};

		//打开窗口
		$rootScope.open = function(row){
			console.log('$rootScope.open.row-->',row);
			var appid = row.sTypeID;
			var url = row.sProcess;
			var name = row.sTypeName;
			var winParams = {};
			winParams.fID = row.sData1;
			winParams.billType = row.sData2;
			winParams.billTypeName = row.sData3;
			winParams.flowTaskID = row.fID//任务ID;
			/*
			var sName = row.sName;
			if(sName.indexOf('审批拒绝')==-1){
				//不存在
				winParams.type='FLOW';
			}else{
				winParams.type='EDIT';
			}
			*/
			winParams.type='FLOW';
			
			$rootScope.menuClick(appid,url,name,winParams)
			
			//如果是通知，则需要在打开之后标记完成
			if(row.sKindID=='tkNotice'){
//					console.log('阅后即焚！');
				var fID = row.fID;
				var data = {};
				data.id = fID;
				
//				$http({
//					method : 'POST'
//					,url : $rootScope.getServiceName()+'/flow/flowTask/finishByID'
//					,data:  data//{'id':rowID,'params':params}
//				}).success(function(data, status, headers, config) {
//				}).error(function(data, status, headers, config) {
//	            	alert('操作失败！\r\n' + '返回的错误代码:' + status);
//				});
				
				$.ajax({
		             type: "POST",
		             url: $rootScope.getServiceName() + "/flow/flowTask/finishByID",
		             data: JSON.stringify(data),
		             dataType: "json",
				     contentType: "application/json; charset=utf-8",
		             success: function(data, textStatus){
		 				console.log('data-->',data);
						$('#show').LoadingOverlay("hide");
						$rootScope.closeCurrentWindow();
	               },error: function(XMLHttpRequest, textStatus, errorThrown){
	            	   $('#show').LoadingOverlay("hide");
	            	   alert('ajax调用失败！/n/r'+data.errText);
	               }
		         });
			}
	
		};

		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		/////////////////////////工作流结束////////////////////////////////////////////////////////////////////////////////////
		
		  
		
		
		return Model;
		
	}])

;
